<?php
// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Model;

use Common\Model\ModelModel;

/**
 * 文章模型
 * @author jry <598821125@qq.com>
 */
class CourseModel extends ModelModel
{
    /**
     * 数据库真实表名
     * 一般为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如微信模块用weixin作为数据表前缀
     * @author jry <598821125@qq.com>
     */
    protected $tableName = 'admin_course';
    public $tag1 = array('1'=>'超高端沙龙','2'=>'品牌沙龙','3'=>'妈妈班','4'=>'mini课程','5'=>'品牌活动','6'=>'多维整合集市','7'=>'直播','8'=>'原创视频','9'=>'线下活动出席','10'=>'kol代言','11'=>'自媒体合作','12'=>'企业团建(含实景植入)');

    /**
     * 自动验证规则
     * @author jry <598821125@qq.com>
     */
    protected $_validate = array(
        /*
        array('part1', 'require', '不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('part2', 'require', '不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('part3', 'require', '内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('part4', 'require', '内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('part5', 'require', '内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('part6', 'require', '内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('part7', 'require', '内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('part8', 'require', '内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('part9', 'require', '内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('part10', 'require', '内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('part11', 'require', '内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('part12', 'require', '内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('part13', 'require', '内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        */
        // array('part3_upload', 'require', '大纲附件不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('part3_length', 'require', '大纲时长不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('part4_content', 'require', '大纲内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('part4_upload', 'require', '大纲附件不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('part4_length', 'require', '大纲时长不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('part5_content', 'require', '大纲内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('part5_upload', 'require', '大纲附件不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('part5_length', 'require', '大纲时长不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('part6_content', 'require', '大纲内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('part6_length', 'require', '大纲时长不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('part6_advice', 'require', '大纲建议不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        //array('title', '', '标题已经存在', self::VALUE_VALIDATE, 'unique', self::MODEL_BOTH),
    );

    /**
     * 自动完成规则
     * @author jry <598821125@qq.com>
     */
    protected $_auto = array(
        array('create_time', 'time', self::MODEL_INSERT, 'function'),
        array('update_time', 'time', self::MODEL_BOTH, 'function'),
        array('status', '1', self::MODEL_INSERT),
    );

    /**
     * 查找后置操作
     * @author jry <598821125@qq.com>
     */
    protected function _after_find(&$result, $options)
    {
        $result['part3_upload_url'] = $this->multi_upload($result['part3']);
        $result['part4_upload_url'] = $this->multi_upload($result['part4']);
        $result['part5_upload_url'] = $this->multi_upload($result['part5']);
        $result['part6_upload_url'] = $this->multi_upload($result['part6']);
        $result['part7_upload_url'] = $this->multi_upload($result['part7']);
        $result['part8_upload_url'] = $this->multi_upload($result['part8']);
        $result['part9_upload_url'] = $this->multi_upload($result['part9']);
        $result['part10_upload_url'] = $this->multi_upload($result['part10']);
        $result['part11_upload_url'] = $this->multi_upload($result['part11']);
        $result['part12_upload_url'] = $this->multi_upload($result['part12']);
        $result['part13_upload_url'] = $this->multi_upload($result['part13']);
        $result['tag1txt']  = $this->tag1[$result['part1']];


    }

    private function multi_upload($ids) {
        $temp = explode(',', $ids);
        foreach ($temp as $value) {
            $t_upload = D('Admin/Upload')->getUploadInfo($value);
            $url = U(MODULE_NAME . '/Upload/download',array('token' => $t_upload['download_token']));
            $data.= "<a href='".$url."'>".$t_upload["name"]."</a><br>";
            //$data[] = D('Admin/Upload')->getUploadInfo($value);    
        }
        return $data;
    }

    

    /**
     * 查找后置操作
     * @author jry <598821125@qq.com>
     */
    protected function _after_select(&$result, $options)
    {
        foreach ($result as &$record) {
            $this->_after_find($record, $options);
        }
    }

    /**
     * 获取列表
     * @author jry <598821125@qq.com>
     */
    public function getList($cid, $limit = 10, $page = 1, $order = null, $map = null)
    {
        $con["status"] = array("eq", '1');
        if ($map) {
            $map = array_merge($con, $map);
        }
        if (!$order) {
            $order = 'id desc';
        }
        $list = $this->page($page, $limit)
            ->order($order)
            ->where($map)
            ->select();

        return $list;
    }
}
